# author: axin
# date: 2021/4/20 17:52
# description:
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.views import APIView
from datetime import date, timedelta

# import goods
from goods.models import GoodsVisitCount
from meiduo_admin.serialziers.statistical import GoodsDayViewSerializer
from users.models import User


class UserCountView(APIView):
    """
        用户总量统计
    """
    # 权限指定
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 获取当天日期datetime
        now_date = date.today()
        # 获取用户总量user
        count = User.objects.all().count()
        # 返回结果
        return Response({
            'date': now_date,
            'count': count
        })


class UserDayCountView(APIView):
    """
        日增用户统计
    """
    # 权限指定
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 获取当天日期datetime
        now_date = date.today()
        # 获取当天注册用户总量user
        # django ORM __gte 大于等于 __gt 大于 __lt小于 __lte小于等于
        count = User.objects.filter(date_joined__gte=now_date).count()
        # 返回结果
        return Response({
            'date': now_date,
            'count': count
        })


class UserActiveCountView(APIView):
    """
        日活用户登录
    """
    # 权限指定
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 获取当天日期datetime
        now_date = date.today()
        # 获取当天登录用户总量user
        # django ORM __gte 大于等于 __gt 大于 __lt小于 __lte小于等于
        count = User.objects.filter(last_login__gte=now_date).count()
        # 返回结果
        return Response({
            'date': now_date,
            'count': count
        })


class UserOrderCountView(APIView):
    """
        日下单用户
    """
    # 权限指定
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 获取当天日期datetime
        now_date = date.today()
        # 获取当天登录用户总量user
        # django ORM __gte 大于等于 __gt 大于 __lt小于 __lte小于等于
        # set 集合 里面数据不能重复，原代码查询的是订单数量的所有用户的数量（有重复），而非下单用户数量，所以用set集合来取出，并计算长度返回
        # count = User.objects.filter(orders__create_time__gte=now_date).count()
        count = len(set(User.objects.filter(orders__create_time__gte=now_date)))
        # 返回结果
        return Response({
            'date': now_date,
            'count': count
        })


class UserMonthCountView(APIView):
    """
        月增用户统计
    """
    # 权限指定
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 获取当天日期
        now_date = date.today()
        # 获取一个月之前的日期
        begin_date = now_date - timedelta(days=29)

        # 创建空列表保存每天的用户量
        date_list = []

        # 遍历
        for i in range(30):
            # 起始日期
            index_date = begin_date + timedelta(days=i)
            # 第二天日期
            next_date = begin_date + timedelta(days=i + 1)

            count = User.objects.filter(date_joined__gte=index_date, date_joined__lt=next_date).count()
            date_list.append({
                'count': count,
                'date': index_date
            })

        return Response(date_list)


class GoodsDayView(APIView):
    """
        日分类商品访问量
    """
    # 权限指定
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 获取当天日期datetime
        now_date = date.today()
        # 获取当天登录用户总量user
        # django ORM __gte 大于等于 __gt 大于 __lt小于 __lte小于等于
        goods = GoodsVisitCount.objects.filter(date__gte=now_date)

        # 生成序列化对象
        ser = GoodsDayViewSerializer(goods, many=True)

        return Response(ser.data)
